package com.hornsun.role.service;

import com.hornsun.data.dbo.Permission;
import com.hornsun.data.dbo.Role;
import com.hornsun.data.repository.PermissionRepository;
import com.hornsun.data.repository.RoleRepository;
import com.hornsun.role.dto.RoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 编译中... on 2018/7/19.
 */
@Service
public class RoleService {
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private PermissionRepository permissionRepository;

    public List<RoleDTO> getAllRoleInfo(){
        List<Permission> permissionList = permissionRepository.getTotal();
        List<Role> roleList = roleRepository.getTotalRole();
        List<RoleDTO> roleDTOList = new ArrayList<>();
        for(Role role : roleList){
            RoleDTO roleDTO = new RoleDTO();
            roleDTO.setRoleName(role.getRoleName());
            roleDTO.setRoleGuid(role.getRoleGuid());
            List<Permission> perList = roleDTO.getPermissionList();
            for(Permission permission : permissionList){
                int permissionInfo = role.getPermission();
                int sign = permission.getSign();
                if ((permissionInfo&sign)!=0){
                    perList.add(permission);
                }
            }
            roleDTOList.add(roleDTO);
        }
        return roleDTOList;
    }

    public boolean whetherSuper(String roleGuid) {
        Role role = roleRepository.getByGuid(roleGuid);
        return role != null && role.getWhetherSuper() == 1;
    }

    public String getRoleName(String roleGuid){
        Role role = roleRepository.getByGuid(roleGuid);
        if (role==null){
            return null;
        }
        return role.getRoleName();
    }
}
